# coding: utf-8
# Author: legend
# @Time： 2020/7/20 下午10:23
from pathlib import Path

from ngs import ssl as ngs_ssl

from config import CONFIG

BASE_DIR = Path(__file__)

SECRET_KEY = '*md7g_rp(i#*@72@5vj$+!y*h(t(x$*0c858c38l@7=$!jsb-3'

DEBUG = True

ALLOWED_HOSTS = ["*"]

NGS_FOLDER = '/opt/ngs/'
HOST_FILE = '/etc/hosts'

PRIORITY_IP_TYPE = CONFIG.priority_ip_type

# host
HOST_IP = CONFIG.webserver.get_address_by_priority_type(PRIORITY_IP_TYPE)
HOST_IPV4 = CONFIG.webserver.ipv4
HOST_IPV6 = CONFIG.webserver.ipv6
HOST_PORT = CONFIG.webserver.port

# mysql
MYSQL_HOST = CONFIG.mysql.get_address_by_priority_type(PRIORITY_IP_TYPE)
MYSQL_IPV4 = CONFIG.mysql.ipv4
MYSQL_IPV6 = CONFIG.mysql.ipv6
MYSQL_PORT = CONFIG.mysql.port
MYSQL_USER = CONFIG.mysql.user
MYSQL_PASSWORD = CONFIG.mysql.password
MYSQL_NAME = CONFIG.mysql.name

INFLUXDB_QUERY_TYPE = CONFIG.influx_query_type

# influxdb
INFLUXDB_HOST = CONFIG.influxdb.get_address_by_priority_type(PRIORITY_IP_TYPE)
INFLUXDB_IPV4 = CONFIG.influxdb.ipv4
INFLUXDB_IPV6 = CONFIG.influxdb.ipv6
INFLUXDB_PORT = CONFIG.influxdb.port
INFLUXDB_USER = CONFIG.influxdb.username
INFLUXDB_PASSWORD = CONFIG.influxdb.password
INFLUXDB_DATABASE = CONFIG.influxdb.db_name
INFLUXDB_RETENTION_POLICY = CONFIG.influxdb.retention_policy
INFLUXDB_TIMEOUT = 5
INFLUXDB_SSL = True
INFLUXDB_VERIFY_SSL = ngs_ssl.CA_FILE
INFLUXDB_USE_THREADING = False
INFLUXDB_TAGS_HOST = ''

# redis
REDIS_IP = CONFIG.redis.get_address_by_priority_type(PRIORITY_IP_TYPE)
REDIS_IPV4 = CONFIG.redis.ipv4
REDIS_IPV6 = CONFIG.redis.ipv6
REDIS_PORT = CONFIG.redis.port
REDIS_PASSWORD = CONFIG.redis.password
REDIS_DB = CONFIG.redis.db

# BIND_CORES = config.get('bind_cores')  # 默认不绑定

LOG_DIR = CONFIG.log.dir

RUNNING_LOG_NAME = CONFIG.log.running_log_name
ERROR_LOG_NAME = CONFIG.log.error_log_name
QUERY_LOG_NAME = CONFIG.log.query_log_name

RUNNING_LOG = Path(LOG_DIR, RUNNING_LOG_NAME)
ERROR_LOG = Path(LOG_DIR, ERROR_LOG_NAME)
QUERY_LOG = Path(LOG_DIR, QUERY_LOG_NAME)

APP_STORE_URL_TEMPLATE = f"https://{HOST_IP}:{HOST_PORT}/app_store/"
REDIS_NAMESPACE = 'oplatform'
REDIS_LOGIN_USER_KEY = '{0}_user_'.format(REDIS_NAMESPACE) + '{user_id}'
REDIS_LOGIN_TOKEN_KEY = '{0}_user_token_'.format(REDIS_NAMESPACE) + '{token}'

REDIS_NAMESPACE = 'oplatform'
REDIS_BYBASE_SEND_QUEUE_KEY = '{0}_send_queue'.format(REDIS_NAMESPACE)
REDIS_BYBASE_PUBLISH_KEY = REDIS_NAMESPACE + '_{packet_code}'
LOGIN_CHANNEL_GROUP_NAME = 'login_{username}'
WARNING_CHANNEL_GROUP_NAME = 'warning'
MESSAGE_SERVER_GROUP_NAME = f'{REDIS_NAMESPACE}_message_server'
USER_CHANNEL_GROUP_NAME = f'{REDIS_NAMESPACE}_user_channel_' + '{user_id}'
COLLECT_SERVICE_CHANNEL_GROUP = f'{REDIS_NAMESPACE}_collect_services'
COLLECTING_SERVICES_KEY = f'{REDIS_NAMESPACE}_collecting_services'
COLLECTED_SERVICES_KEY = f'{REDIS_NAMESPACE}_collected_services'
COLLECTED_DEVICES_KEY = f'{REDIS_NAMESPACE}_collected_devices'
DEVICES_TO_COLLECT_KEY = f'{REDIS_NAMESPACE}_devices_to_collect'
DEVICE_INFO_KEY = f'{REDIS_NAMESPACE}_device_info_' + '{device_id}'
SERVICE_INFO_KEY = f'{REDIS_NAMESPACE}_service_info_' + '{service_id}'
BUSINESS_INFO_KEY = f'{REDIS_NAMESPACE}_business_info_' + '{app_id}'
BYBASE_MESSAGE_KEY = f'{REDIS_NAMESPACE}_bybase_' + '{msg_id}'
UPGRADE_APP_INFO_KEY = f'{REDIS_NAMESPACE}_app_upgrade_info'
MESSAGE_ROUTER = 'websocket.message_router'

CHANNEL_LAYERS_REDIS_URL = "redis://:{}@{}:{}/0".format(REDIS_PASSWORD, REDIS_IP, REDIS_PORT)

models = [
    'aerich.models',
    'apps.iacs.models',
    'apps.log.models',
    'apps.resource.models',
    'apps.statistic.models',
    'apps.alarm.models',
]

TORTOISE_ORM = {
    "connections": {"default": f'mysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_NAME}'},
    "apps": {
        "models": {
            "models": models,
            # 须添加“aerich.models” 后者“models”是上述models.py文件的路径
            "default_connection": "default",
        },
    },
}
